import axios from 'axios'
import { Toast, Indicator } from 'mint-ui';
import { mapState } from 'vuex'

const data = {
  userId: JSON.parse(window.localStorage.getItem('userData')) ?  JSON.parse(window.localStorage.getItem('userData')).user_id : null,
  token: JSON.parse(window.localStorage.getItem('userData')) ?  JSON.parse(window.localStorage.getItem('userData')).token : null,
  companyId: JSON.parse(window.localStorage.getItem('userData')) ?  JSON.parse(window.localStorage.getItem('userData')).company_id : null,
  depId: JSON.parse(window.localStorage.getItem('userData')) ?  JSON.parse(window.localStorage.getItem('userData')).depId : null
};
const service = axios.create({
  baseURL: process.env.API_ROOT,
  // timeout: 10000,
  params: data
})

// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent
    Indicator.open({
      text: '数据加载中...',
    });
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// respone interceptor
service.interceptors.response.use(
  response => {
    Indicator.close();
    return response;
  },

  error => {
    Toast('服务器获取数据失败，请稍后再试...  ')
    Indicator.close();
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service
